import { defineStore } from 'pinia';
import { mine } from '@/enums/constants';
import { jump } from '@/utils/index'
import indexApi from "@/apis/index";
import { userStore } from "@/stores";

export const homeStore = defineStore('home', {
    state: () => {
        return {
            storeInfo:uni.getStorageSync('storeInfo') || {},
            personList:[]
        };
    },
    //persist:true,
    //多端处理
    /*persist:{
      enabled:true,
      storage:{
        setItem(key,value){
          uni.setStorageSync(key,value);
        },
        getItem(key){
          return uni.getStorageSync(key);
        }
      },
    },*/
    actions: {
        //获取商店
        async checkStoreStatus(isJump,loginSuccessRedirectUrl){
            const res = await indexApi.checkStoreStatus()
            if(res.code == 'SUCCESS'){
                const store = userStore();
                let obj = {
                    status:'store not find'
                }
                if(res.data == 'store not find'){
                    obj.status = 'store not find'
                    this.setInfoStore(obj);
                    //未绑定邀请员工跳转
                    console.log(loginSuccessRedirectUrl);

                    if(isJump){
                        if(loginSuccessRedirectUrl){
                            jump(loginSuccessRedirectUrl);
                        }else{
                            //首次进入创建店铺
                            uni.reLaunch({
                                url:`/pages/index/verify`,
                            });
                        }
                    }

                }
                if(res.data.status == 'SUBMIT' || res.data.status == 'REJECT'){
                    res.data.isRoleType = res.data.userId == store.userInfo.id;
                    obj.status = res.data.status;
                    this.setInfoStore(res.data);
                    isJump && uni.reLaunch({
                        url:`/pages/index/verify`,
                    });
                }
                if(res.data.status == 'PASS') {
                    res.data.isRoleType = res.data.userId == store.userInfo.id;
                    obj.status = res.data.status;
                    this.setInfoStore(res.data);

                    if (isJump) {
                        if(loginSuccessRedirectUrl){
                            jump(loginSuccessRedirectUrl);
                        }else if(!res.data.isRoleType){
                            uni.reLaunch({
                                url: `/pages/index/dmView`,
                            });
                        }else{
                            uni.reLaunch({
                                url: `/pages/index/index`,
                            });
                        }
                    }
                }
                return obj;
            }

        },
        //清除数据
        clearStore() {
            this.storeInfo= {};
        },

        setInfoStore(info) {
            this.storeInfo = info;
            uni.setStorage({
                key: 'storeInfo',
                data: info,
            });
        },
        //获取人员列表
        getListWorks(){
            let para = {
                storeId:this.storeInfo.id,
            }
            indexApi.getListWorks(para).then(res=>{
                if(res.code == 'SUCCESS'){
                    if(res.data.length){
                        this.personList = res.data.sort((a, b) => {
                            if (a.role === "ADMIN") {
                                return -1; // a排在b前面
                            } else if (b.role === "admin") {
                                return 1; // b排在a前面
                            } else {
                                return 0; // 保持原顺序
                            }
                        });
                    }else{
                        this.personList = [];
                    }
                }
            })
        }

    }
});
